/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.actionControll.observe {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLActionClass;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.data.MLCollection;
	import com.googlecode.milib.vo.MLEvent;
	import com.googlecode.milib.vo.MLEventInfo;

	/**
	 * @author Marek Brun
	 */
	public class AnyActionIsRunningObserver extends MLBroadcastClass implements IMLBroadcasterListener {
		
		public const event_SomeActionIsRunning : String = 'SomeActionIsRunning';		public const event_AllActionsIsNotRunning : String = 'AllActionsIsNotRunning';
		
		public var collection:MLCollection;
		private var countRunningActions:uint=0;
		private var isSomeActionRunning:Boolean;

		
		public function AnyActionIsRunningObserver() {
			collection=new MLCollection(null, null, MLActionClass);
			collection.addListener(this);
			isSomeActionRunning=false;
		}
		
		protected function setIsSomeActionRunning(someActionRunning:Boolean):void {
			if(isSomeActionRunning==someActionRunning){ return; }
			isSomeActionRunning=someActionRunning;
			if(isSomeActionRunning){
				broadcastEvent(event_SomeActionIsRunning);
			}else{
				broadcastEvent(event_AllActionsIsNotRunning);
			}
		}
		
		protected function setSomeActionIsRunning():void {
			countRunningActions++;
			setIsSomeActionRunning(true);
		}
		
		protected function setSomeActionIsNotRunning():void {
			countRunningActions--;
			setIsSomeActionRunning(countRunningActions>0);
		}
		
//********************************************************************************************
//		events for AnyActionIsRunningObserver
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case collection:
					var action:MLActionClass;
					switch(ev.event) {
						case collection.event_ValueAdded:
							action=MLActionClass(ev.data);
							action.addEventListener(action.event_ActionStart, onActionActionStart);
							action.addEventListener(action.event_ActionFinish, onActionActionFinish);
							if(action.getIsRunning()){
								setSomeActionIsRunning();
							}
						break;
						case collection.event_ValueRemoved:
							action=MLActionClass(ev.data);
							action.removeEventListener(action.event_ActionStart, onActionActionStart);
							action.removeEventListener(action.event_ActionFinish, onActionActionFinish);
							if(action.getIsRunning()){
								setSomeActionIsNotRunning();
							}
						break;
						case collection.event_Cleared:
							setIsSomeActionRunning(false);
						break;
					}
				break;
			}
		}
		
		protected function onActionActionStart(event:MLEvent):void {
			setSomeActionIsRunning();
		}
		
		protected function onActionActionFinish(event:MLEvent):void {
			setSomeActionIsNotRunning();
		}
		
	}
}
